<?php 
/**
 * 
 */
header("Content-Type: text/html; charset=UTF-8");
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends CI_Controller {
	private $user_id;//用户id
	private $username;//用户名
	private $base_url;//首页
	function __construct()
	{
		parent::__construct();	
		$this->user_id = $this->session->userdata('user_id');
		$this->username = $this->session->userdata('username');
		$this->base_url = base_url();
		/*$method = $this->uri->segment(2);
		if(!method_exists($this,$method)){
			redirect('home/index/','location');
			exit;
		}*/
	}
	/**
	 * 魔术函数没起作用 很是奇怪
	 */
	function __call($method,$args)
	{
		print_r($args);
	}
	/**
	 * 首页
	 */
	public function index()
	{
		$data['name'] = empty($this->username)?'游客':$this->username;
		$this->load->view('home/index',$data);
	}
	/**
	 * 注册
	 */
	function regist()
	{
		if( !is_post() ){
			$data['regist_coin'] = get_base('regist_coin');
			$this->load->view('home/regist',$data);
		}else{
			$this->load->helper('email');
			if (isset($_POST['ck'])){
				if ($_POST['ck'] == 'username'){
					$username = $_POST['username'];
					$name_len = strlen($username);
					if( $name_len<4 or $name_len>20 ){
						error_return("用户名为4-20个字符");
					}
					$ckusername = $this->ckusername($username);
					if ($ckusername > 0){
						error_return("用户名已存在");
					}else {
						success_return("可以使用");
					}
				}elseif ($_POST['ck'] == 'email'){
					$email = $_POST['email'];
					if(!valid_email($email)){
						error_return("邮箱格式不正确");
					}
					$ckemail = $this->ckemail($email);
					if ($ckemail > 0){
						error_return("邮箱已存在");
					}else {
						success_return("可以使用");
					}
				}
			} else {
				$username = $_POST['username'];
				$password = $_POST['password'];
				$email = $_POST['email'];
				if (empty($username)){
					error_return("用户名不能为空");
				}
				$name_len = strlen($username);
				if( $name_len<4 or $name_len>20 ){
					error_return("用户名为4-20个字符");
				}
				if (empty($password)){
					error_return("密码不能为空");
				}
				if (empty($email)){
					error_return("Email不能为空");
				}
				if(!valid_email($email)){
					error_return("邮箱格式不正确");
				}
				if ($password != $_POST['confirm_pwd']){
					error_return("两次密码不一致");
				}
				$ckusername = $this->ckusername($username);
				if ($ckusername > 0){
					error_return("用户名已存在");
				}
				$ckemail = $this->ckemail($email);
				if ($ckemail > 0){
					error_return("邮箱已存在");
				}
				$password = md5(md5($password).$this->pwd_salt);
				$ip = $this->input->ip_address();
				$time = time();
				$insert = array();
				$insert['name'] = $username;
				$insert['password'] = $password;
				$insert['email'] = $email;
				$insert['regist_time'] = $time;
				$insert['regist_ip'] = $ip;
				$insert['last_login_time'] = $time;
				$insert['coin'] = 0;
				$insert['max_coin'] = 0;
				$this->db->insert('user',$insert);
				$user_id = $this->db->insert_id();
				$ary_session = array(
					'user_id'=>$user_id,
					'username'=>$username,
				);
				$this->session->set_userdata($ary_session);
				$regist_coin = get_base('regist_coin');
				$this->common->add_coin($user_id,'regist',$regist_coin);
				success_return("注册成功!获得{$regist_coin}D币");
			}
		}
	}
	/**
	 * 检查用户名是否存在
	 */
	function ckusername($username){
		$query = $this->db->select('id')->where('name',$username)->get('user');
		return $query->num_rows();
	}
	/**
	 * 检查邮箱是否存在
	 */
	function ckemail($email){
		$query = $this->db->select('id')->where('email',$email)->get('user');
		return $query->num_rows();
	}
	/**
	 * 登录
	 */
	function login()
	{
		//已经登录 跳转到个人页面？
		if( !empty($this->user_id) ){
			echo "<script language='javascript'>window.location.href='{$this->base_url}';</script>";
			exit;
		}
		if( !is_post() ){
			$data['user_id'] = $this->user_id;
			$this->load->view('home/login',$data);
		}else{
			$username = trim($_POST['username']);
			$password = trim($_POST['password']);
			if (empty($username)){
				error_return("用户名不能为空");
			}
			if (empty($password)){
				error_return("密码不能为空");
			}
			$query_user = $this->db->select('id,password')->where('name',$username)->get('user');
			if ($query_user->num_rows()==0){
				error_return("用户名或密码错误");
			}else{
				$row_user = $query_user->row();
				if (md5(md5($password).$this->pwd_salt) != $row_user->password){
					error_return("用户名或密码错误");
				}else {
					$ary_session = array(
						'user_id'=>$row_user->id,
						'username'=>$username,
					);
					$this->session->set_userdata($ary_session);
				}
			}
			success_return("登录成功!");
		}
	}
	/**
	 * 取回密码
	 */
	function forgot_pwd()
	{
		if (!is_post()){
			$this->load->view('home/forgot_pwd');
		}else{
			//判断邮箱是否存在
			$username = trim($_POST['username']);
			$email = trim($_POST['email']);
			$where = array(
				'name'=>$username,
				'email'=>$email,
			);
			$query_user = $this->db->select('id')->where($where)->get('user');
			if ($query_user->num_rows()==0){
				error_return("用户名或者邮箱不匹配");
			}else{
				$row_user = $query_user->row();
				$new_password = substr(uniqid(rand()), -6);
				$password = md5(md5($new_password).$this->pwd_salt);
				$data = array(
	               'password'=>$password,
	            );
				$this->db->where('id', $row_user->id);
				$this->db->limit(1);
				$this->db->update('user', $data);
				sendmail_by_phpmailer($email,"dd网密码修改","您的新密码为{$new_password},
				请尽快登陆并且修改");
				success_return("邮件已发出");
			}
		}
	}
	/**
	 * 服务条款
	 */
	function server_text()
	{
		$this->load->view('home/server_text');
	}
	/**
	 * 提交建议
	 */
	function suggest()
	{
		if (!is_post()){
			//判断登录
			if( empty($this->user_id) ){
				$login_url = site_url(array('home','login'));
				echo "<script language='javascript'>alert('请先登录');window.location.href='{$login_url}';</script>";
				exit;
			}
			$this->load->view('home/suggest');	
		}else{
			if( empty($this->user_id) ){
				error_return("先登录再提交哦，很有可能有积分拿哦。");
			}
			$type = $_POST['type'];
			$title = $_POST['title'];
			$content = $_POST['content'];
			if( empty($type) OR empty($title) OR empty($content) ){
				error_return("提交信息不完整");
			}
			$insert = array();
			$insert['type'] = $type;
			$insert['title'] = $title;
			$insert['content'] = $content;
			$insert['add_time'] = time();
			$this->db->insert('suggest',$insert);
			success_return('提交成功!我们将尽快处理你反馈的意见。');
		}
	}
}
